package com.edu.lxshpc.controller;

import com.edu.lxshpc.pojo.Blog;
import com.edu.lxshpc.povo.BlogCommentVO;
import com.edu.lxshpc.povo.BlogVO;
import com.edu.lxshpc.service.BlogCategoryService;
import com.edu.lxshpc.service.BlogService;
import com.edu.lxshpc.utils.PageParams;
import com.edu.lxshpc.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 博客文章
 * @author zgh
 * @date 2023/10/28
 *
 */
@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {

    @Autowired
    private BlogCategoryService blogCategoryService;
    @Autowired
    private BlogService blogService;

    /**
     * 获取文章列表
     * @param page 当前页
     * @param pageSize 页面大小
     * @param categoryId 分类id
     * @param id 用户id
     * @param search 模糊查询字段
     * @return 业务结果
     */
    @GetMapping("/list")
    public Result getArticleList(@RequestParam(required = false) Integer page,
                                 @RequestParam(required = false) Integer pageSize,
                                 @RequestParam(required = false) Long id,
                                 @RequestParam(required = false) Integer categoryId,
                                 @RequestParam(required = false) String search) {
        PageParams pageParams = null;
        log.info("page:{},pageSize:{},categoryId:{},id:{},search:{}",page,pageSize,categoryId,id,search);
        if (Objects.isNull(page) || Objects.isNull(pageSize)) {
            pageParams = new PageParams();
        }else {
            pageParams = new PageParams(page,pageSize);
        }
        return blogService.getArticleList(pageParams,id,categoryId,search);
    }

    /**
     * 查询博客文章分类列表
     * @return 返回数据列表
     */
    @GetMapping("/category")
    public Result getCategory() {
        return blogCategoryService.getBlogCategoryList();
    }

    /**
     * 获得博客标题列表
     * @return 业务结果信息
     */
    @GetMapping("/blogTitle")
    public Result getBlogTitle() {
        return blogService.getBlogTitleList();
    }

    /**
     * 根据分类id查询博客文章数据
     * @return 返回文章分类下的数据列表
     */
    @GetMapping("/category/{id}")
    public Result getCategoryById(@PathVariable Integer id) {
        if (Boolean.TRUE.equals(Objects.nonNull(id))) {
            return blogCategoryService.getBlogListByCategory(id);
        }
        return Result.fail("分类错误");
    }

    /**
     * 根据博客id获取文章详情
     * @param blogId 文章Id
     * @return Result对象，包含文章详情或错误信息
     */
    @GetMapping("/detail")
    public Result getArticleDetail(@RequestParam String blogId) {
        if (Boolean.TRUE.equals(Objects.nonNull(blogId))) {
            return blogService.getBlogDetail(Long.parseLong(blogId));
        }
        return Result.fail("文章不存在");
    }

    /**
     * 插入博客文章
     * @param blogVO blogVO实体
     * @return 业务信息
     */
    @PostMapping("/insert")
    @PreAuthorize("hasAnyRole('USER','ADMIN')")
    public Result uploadBlog(@RequestBody BlogVO blogVO) {
        return blogService.saveOrUpdateBlog(blogVO);
    }



    /**
     *  插入评论
     * @param blogCommentVO 评论vo类
     * @return 业务信息
     */
    @PostMapping("/comment")
    @PreAuthorize("hasAnyRole('USER','ADMIN')")
    public Result insertBlogComment(@RequestBody(required = true) BlogCommentVO blogCommentVO) {
        return blogService.saveBlogComment(blogCommentVO);
    }

    /**
     * 删除评论
     * @param commentId 评论id
     * @return 业务结果信息
     */
    @DeleteMapping("/comment/{commentId}")
    @PreAuthorize("hasAnyRole('USER','ADMIN')")
    public Result deleteBlogComment(@PathVariable Long commentId) {
        return blogService.removeCommentById(commentId);
    }

    /**
     * 获得博客评论列表
     * @param blogId 博客id
     * @param page 当前页
     * @param pageSize 页面大小
     * @return 业务结果信息
     */
    @GetMapping("/comments")
    public Result getBlogComments(@RequestParam(required = false) Long blogId,
                                 @RequestParam(required = false) Integer page,
                                 @RequestParam(required = false) Integer pageSize) {
        PageParams pageParams = null;
        if (Objects.isNull(page) || Objects.isNull(pageSize)) {
            pageParams = new PageParams();
        }else {
            pageParams = new PageParams(page,pageSize);
        }
        return blogService.getBlogComments(blogId,pageParams);
    }

    /**
     * 更新博客内容
     * @param blogVO 实体类
     * @return 业务信息
     */
    @PutMapping("/update")
    @PreAuthorize("hasAnyRole('USER','ADMIN')")
    public Result updateBlog(@RequestBody(required = true) BlogVO blogVO) {
        if (Boolean.TRUE.equals(Objects.nonNull(blogVO.getBlogId()) && blogVO.getBlogId() > 0)) {
            return blogService.saveOrUpdateBlog(blogVO);
        }
        return Result.fail("更新失败");
    }

    /**
     *  删除博客数据
     * @param blogId 博客id
     * @return 业务信息
     */
    @DeleteMapping("/delete/{blogId}")
    @PreAuthorize("hasAnyRole('USER','ADMIN')")
    public Result deleteBlog(@PathVariable(name = "blogId") Long blogId) {
        if (Boolean.TRUE.equals(Objects.nonNull(blogId))) {
            return blogService.removeById(blogId);
        }
        return Result.fail("删除失败");
    }

}
